package com.rest.demo.nio.execrise;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-11 10:49
 */
public class FileChannelDemo {


    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(new File("F:\\springboot\\sell\\src\\main\\java\\com\\imooc\\controller\\BuyerOrderController.java"));
        FileChannel fileChannel = fis.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        int b = -1;
        StringBuffer sb = new StringBuffer();
        while((b = fileChannel.read(byteBuffer)) != -1) {
            fileChannel.read(byteBuffer);
            byteBuffer.flip();
            while(byteBuffer.hasRemaining()) {
                sb.append(((char) byteBuffer.get()));
            }
            byteBuffer.clear();
        }
        System.out.println(sb.toString());
        fis.close();

        System.out.println("----------------------------");

        //inputStream
        InputStream is = new FileInputStream(new File("F:\\springboot\\sell\\src\\main\\java\\com\\imooc\\controller\\BuyerOrderController.java"));
        StringBuffer sb2 = new StringBuffer();
        byte[] buffer = new byte[1024];
        int res = -1;
        while((res = is.read(buffer)) != -1) {
            sb2.append(new String(buffer,0,res));
        }
        System.out.println(sb2.toString());
        is.close();
    }
}
